package com.foreknow.hospital.service.impl;

import com.foreknow.hospital.dao.OrdersDao;
import com.foreknow.hospital.dao.impl.OrdersDaoImpl;
import com.foreknow.hospital.po.Orders;
import com.foreknow.hospital.service.OrdersService;
import com.foreknow.hospital.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {

        //建立连接
        Connection conn= DBUtil.getConnection();
        PreparedStatement pst;
        @Override
        public int getOrdersByUserId(Orders order) {
            int state =Integer.MAX_VALUE;
            OrdersDao ordersDao = new OrdersDaoImpl();
            try{
                //调用Dao层的方法
                state=ordersDao.getOrdersByUserId(order);
            }
            catch(Exception e){
            }
            finally {
                //关闭资源
                DBUtil.close(pst);
            }
            return state;

        }

        @Override
        public int saveOrders(Orders order) {
            int result=0;
            OrdersDao ordersDao = new OrdersDaoImpl();
            try {
                //开始事务
                DBUtil.beginTransaction();
                result = ordersDao.saveOrders(order);
                //提交事务
                DBUtil.commitTransaction();
            }  catch (Exception e) {
                try {
                    //取消之前的所有操作
                    DBUtil.rollbackTransaction();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                throw new RuntimeException(e);
            }finally {
                DBUtil.close();
            }
            return result;
        }

        @Override
        public int removeOrders(Orders order) {
            int result=0;
            OrdersDao ordersDao = new OrdersDaoImpl();
            try {
                //开始事务
                DBUtil.beginTransaction();
                result=ordersDao.removeOrders(order);
                //提交事务
                DBUtil.commitTransaction();
            }  catch (Exception e) {
                try {
                    //取消之前的所有操作
                    DBUtil.rollbackTransaction();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                throw new RuntimeException(e);
            }finally {
                DBUtil.close();
            }
            return result;
        }

        @Override
        public Orders getOrdersById(Orders order) {
            Orders orders=new Orders();
            OrdersDao ordersDao = new OrdersDaoImpl();
            try{
                //调用Dao层的方法
                orders=ordersDao.getOrdersById(order);
            }
            catch(Exception e){
            }
            finally {
                //关闭资源
                DBUtil.close(pst);
            }
            return orders;
        }

        @Override
        public List<Orders> listOrdersByUserId(Orders order) {
            List<Orders> list=new ArrayList<>();
            OrdersDao ordersDao = new OrdersDaoImpl();
            try{
                //调用Dao层的方法
                list=ordersDao.listOrdersByUserId(order);
            }
            catch(Exception e){
            }
            finally {
                //关闭资源
                DBUtil.close(pst);
            }
            return list;


        }
    }


